<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/12/12
 * Time: 17:03
 */
namespace common\logics\reptile;

use common\widgets\Tool;
use yii\base\BaseObject;

class ReptileLogic extends BaseObject implements IReptile
{

    public function getMessage($data)
    {
        header("Content-type: text/html; charset=gbk");
        if(empty($data['url'])){
            return Tool::returnJson([],401,"爬虫连接不能为空");
        }
        if(empty($data['type'])){
            return Tool::returnJson([],402,"类型不能为空");
        }
        //判断是否是url
        if(!Tool::isUrl($data['url']))
        {
            return Tool::returnJson([],403,"该链接不是正常的链接");
        }

        if($data['type'] == 1)
        {
            $arr  = explode("taobao",$data['url']);
            if(count($arr)<2)
            {
                return Tool::returnJson([],405,"非淘宝平台链接");
            }
            $taobao = $this->getTaoBaoMessage($data);
            if(empty($taobao))
            {
                return Tool::returnJson([],406,"抓取淘宝数据失败");
            }
            return Tool::returnJson($taobao,200,"成功");
        }
        if($data['type'] == 2)
        {
            $arr  = explode("tmall",$data['url']);
            if(count($arr)<2)
            {
                return Tool::returnJson([],407,"非天猫平台链接");
            }
          $tmall = $this->getTmallMessage($data);
          if(empty($tmall)){
              return Tool::returnJson([],408,"抓取天猫数据失败");
          }
          return Tool::returnJson($tmall,200,"成功");
        }
        return Tool::returnJson([],409,"抓取淘宝数据失败");
    }

    private function getTaoBaoMessage($data)
    {
        header("Content-type: text/html; charset=gbk");
        //测试爬虫 z抓取淘宝数据
        $content = file_get_contents($data['url']);
        preg_match('/<(div)[^c]*class=\"tb-detail-bd tb-clear\"[^>]*>.*<\/\\1>/is', $content, $text0);
        //获取标题
        preg_match("/<h3.*?class=\".*?tb-main-title.*?\".*?>(.*?)<\/h3>/is", $content, $titles);
        $title = '';
        if(!empty($titles)){
            $title = strip_tags($titles[1]);
        }
        //获取价格
        preg_match("/<em.*?class=\".*?tb-rmb-num.*?\".*?>(.*?)<\/em>/is", $content, $prices);
        $price = '';
        if(!empty($prices)){
            $price = strip_tags($prices[1]);
        }
        //图片
        preg_match("/<ul.*?id=\".*?J_UlThumb.*?\".*?>(.*?)<\/ul>/is", $content, $arr);
        if(!empty($arr))
        {
            preg_match( "/<li.*?class=\".*?tb-selected.*?\".*?>(.*?)<\/li>/is", $arr[1],$imarr);
            preg_match('/<img[^>]*src=[\'"]?([^>\'"\s]*)[\'"]?[^>]*>/i', $imarr[1], $img);
        }
        $img_url = '';
        if(!empty($img)){
//            $img_url = $img[1];
              $img_arr = explode('_',$img[1]);
              if(count($img_arr)>0)
              {
                  $imArr = explode('.',$img_arr[count($img_arr)-1]);
                  $img_str = '';
                  unset($img_arr[count($img_arr)-1]);
                  $img_str = implode('_',$img_arr);
                  $img_str = $img_str."_"."250x250.".$imArr[count($imArr)-1];
                  $img_url = $img_str;
              }else
              {
                  $img_url = $img[1];
              }
        }
        $dpname = '';
        //抓取店铺信息
        preg_match("/<div.*?class=\".*?tb-shop-name.*?\".*?>(.*?)<\/div>/is", $content, $darr);
        if(!empty($darr)){
            $dpname = strip_tags($darr[1]);
        }

        $wwname = '';
        preg_match("/<a.*?class=\".*?tb-seller-name.*?\".*?>(.*?)<\/a>/is", $content, $warr);
        if(!empty($warr)){
            $wwname = $warr[1];
        }
        $data = [
            'title' => iconv("GBK","UTF-8//IGNORE",trim($title)),
            'price' => iconv("GBK","UTF-8//IGNORE",$price),
            'img_url' =>$img_url,
            'dname' => iconv("GBK","UTF-8//IGNORE",trim($dpname)),
            'wwname' => iconv("GBK","UTF-8//IGNORE",trim($wwname)),
        ];
        return $data;
    }

    private function getTmallMessage($data)
    {
        header("Content-type: text/html; charset=gbk");
        $content = file_get_contents($data['url']);
        preg_match('/<(div)[^c]*class=\"tb-gallery\"[^>]*>.*<\/\\1>/is', $content, $text0);
        //获取标题
        preg_match('/<img[^>]*id="J_ImgBooth"[^r]*rc=\"([^"]*)\"[^>]*>/', $content, $img);
        $url_img = '';
        if(!empty($img)){
            $url_img = $img[1];
        }
        //获取标题
        preg_match('/<(h1)[^c]*data-spm=\"1000983\"[^>]*>.*<\/\\1>/is', $content, $titles);
        $titles = '';
        if(!empty($titles)){
            $titles = strip_tags($titles[0]);
        }
        //获取店铺名称
        preg_match("/<a.*?class=\".*?slogo-shopname.*?\".*?>(.*?)<\/a>/is", $content, $arr);
        $dname = '';
        if(!empty($arr)){
            $dname = strip_tags($arr[1]);
        }
        $data = [
            'title' => iconv("GBK","UTF-8//IGNORE",trim(strip_tags($titles))),
            'price' => '0.00',
            'url_img' =>($url_img),
            'dname' => iconv("GBK","UTF-8//IGNORE",trim(strip_tags($dname))),
            'wwname' => iconv("GBK","UTF-8//IGNORE",trim(strip_tags($dname))),
        ];
        return $data;
    }
}